package com.example.mapstruct.complex_map;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

@Mapper
public interface CarMapper {
    CarMapper INSTANCE = Mappers.getMapper(CarMapper.class);

    // 如果类型不一致，如String和Integer等数值型之间的转换会自动进行，具体可看CarMapperImpl
    // 如果是只将对象中某个属性映射出来可以按下面这样配
    @Mapping(source = "car.customer.name", target = "customerName")
    // 将整个对象属性映射到另一个对象属性的话，就需要另外写一个方法，如方法map进行具体映射
    @Mapping(source = "customer", target = "vip")
    // 指定方法进行转换
    @Mapping(source = "car.customer", target = "user", qualifiedByName = "customerToStringUser")
    CarDTO carToCarDTO(Car car);

    // 上面在转换时就会调用这个方法进行Customer和CustomerDTO之间的转换
    @Mapping(source = "name", target = "customerName")
    CustomerDTO map(Customer customer);

    @Named("customerToStringUser")
    default String customerToStringUser(Customer customer) {
        return customer.toString();
    }
}
